//
//  CommodityCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/27.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "CommodityCell.h"
#import "CommodityView.h"

@interface CommodityCell ()

@property (nonatomic, strong) CommodityView *firstView;
@property (nonatomic, strong) CommodityView *secondView;

@property (nonatomic, strong) CommodityModel *firstModel;
@property (nonatomic, strong) CommodityModel *secondModel;

@end

@implementation CommodityCell

+ (NSArray *)getCellItemArr:(NSArray *)dataArr withIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *arr = [NSMutableArray array];
    
    CommodityModel *firstModel = [dataArr safeObjectAtIndex:(2 * indexPath.row)];
    [arr addObject:firstModel];
    
    if ((dataArr.count - 1) / 2 == indexPath.row) {
        if (dataArr.count % 2 == 0) {
            CommodityModel *secondModel = [dataArr safeObjectAtIndex:(2 * indexPath.row) + 1];
            [arr addObject:secondModel];
            
        }
    } else {
        CommodityModel *secondModel = [dataArr safeObjectAtIndex:(2 * indexPath.row) + 1];
        [arr addObject:secondModel];
    }
    return arr;
}

// 获取一行俩个的数组
+ (NSArray *)getCellItemArr:(NSArray *)dataArr withRow:(NSInteger)row {
    NSMutableArray *arr = [NSMutableArray array];
    
    CommodityModel *firstModel = [dataArr safeObjectAtIndex:(2 * row)];
    [arr addObject:firstModel];
    
    if ((dataArr.count - 1) / 2 == row) {
        if (dataArr.count % 2 == 0) {
            CommodityModel *secondModel = [dataArr safeObjectAtIndex:(2 * row) + 1];
            [arr addObject:secondModel];
            
        }
    } else {
        CommodityModel *secondModel = [dataArr safeObjectAtIndex:(2 * row) + 1];
        [arr addObject:secondModel];
    }
    return arr;
}

+ (NSInteger)getRowNumberWithItemArr:(NSArray *)dataArr {
    
    return ((dataArr.count + 1) / 2);
}

+ (CGFloat)getHeight {
    CGFloat height = 0;
    
    CGFloat width = ScreenWidth - 1;
    CGFloat itemWidth = width * 0.5;
    
    height += [CommodityView getHeightWithWidth:itemWidth];
    height += 1;
    return height;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _firstModel = [[CommodityModel alloc] init];
        _secondModel = [[CommodityModel alloc] init];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self.contentView addSubview:self.firstView];
    [self.contentView addSubview:self.secondView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = ScreenWidth - 1;
    CGFloat itemWidth = width * 0.5;
    
    self.firstView.frame = CGRectMake(0, 1, itemWidth, [CommodityView getHeightWithWidth:itemWidth]);
    self.secondView.frame = CGRectMake(self.firstView.right + 1, 1, itemWidth, [CommodityView getHeightWithWidth:itemWidth]);
}

- (void)setValueForCell:(NSArray *)modelArr {
    self.secondView.hidden = ((modelArr.count % 2) == 1);
    
    
    self.firstModel = [modelArr firstObject];
    self.secondModel = [modelArr lastObject];
    
    [self.firstView setItemViewWithImageUrl:self.firstModel.gimg title:self.firstModel.gname subTitle:self.firstModel.gextension price:self.firstModel.price];
    [self.secondView setItemViewWithImageUrl:self.secondModel.gimg title:self.secondModel.gname subTitle:self.secondModel.gextension price:self.secondModel.price];
    
    __weak typeof(self) weakSelf = self;
    self.firstView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(0);
        }
    };
    self.secondView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(1);
        }
    };
}

- (CommodityView *)firstView {
    if (!_firstView) {
        _firstView = [[CommodityView alloc] init];
    }
    return _firstView;
}

- (CommodityView *)secondView {
    if (!_secondView) {
        _secondView = [[CommodityView alloc] init];
    }
    return _secondView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
